﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.Domain.PermisosModule.Contracts.Distritos;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.IoC;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Juntas
{
	public partial class JuntasViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Distrito> _distritos;

		/// <summary>
		/// 
		/// </summary>
		private Distrito _distrito;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Distrito> Distritos
		{
			get
			{
				return _distritos;
			}
			private set
			{
				_distritos = value;
				if (_distritos != null)
					RaisePropertyChanged("Distritos");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Distrito Distrito
		{
			get
			{
				return _distrito;
			}
			private set
			{
				_distrito = value;
				if (_distrito != null)
					RaisePropertyChanged("Distrito");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsDistritoReady
		{
			get
			{
				return Distrito != null;
			}
		}

		#endregion

		#region Actions

		/// <summary>
		/// 
		/// </summary>
		private void LoadDistritos()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IDistritoService distritoService = IoCFactory.Resolve<IDistritoService>();
					e.Result = distritoService.FindDistritosByEstado(Estado.EstadoGuid);
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Distritos = e.Result as List<Distrito>;
					else if (e.Error != null)
						MessageBox.Show(
							_juntasView,
							e.Error.Message,
							WindowTitles.JuntasWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		private void OnDistritosPropertyChange()
		{
			_juntasView.distritosBindingSource.DataSource = Distritos;
			_juntasView.distritosBindingSource.CurrentChanged += SetCurrentDistrito;
			SetCurrentDistrito(_juntasView.distritosBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentDistrito(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null)
				if (bindingSource.Current != null)
					Distrito = bindingSource.Current as Distrito;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnDistritoPropertychanged()
		{
		}

		#endregion

	}
}
